package de.baumann.browser.fragments

import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.Toast
import de.baumann.browser.R
import de.baumann.browser.activitys.oc.OcWalletDetailActivity
import de.baumann.browser.base.BaseOdinFragment
import de.baumann.browser.db.ETHWallet
import de.baumann.browser.iview.ImportWalletView
import de.baumann.browser.present.ImportWalletPresenter
import de.baumann.browser.utils.saveMoneyAddress
import de.baumann.browser.utils.saveWalletName
import kotlinx.android.synthetic.main.fragment_import_key.*
import kotlinx.android.synthetic.main.fragment_import_keystore.*
import kotlinx.android.synthetic.main.fragment_import_mnemonic.*
import kotlinx.android.synthetic.main.imoprt_mnemonic.*


class ImportMnemonicFragment : BaseOdinFragment<ImportWalletView, ImportWalletPresenter>(), ImportWalletView {

    companion object {
        val instance: ImportMnemonicFragment by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            ImportMnemonicFragment()
        }
    }

    override fun getLayout(): Int {
        return R.layout.fragment_import_mnemonic
    }

    var mc: String = ""

    override fun initView(view: View) {
        tvMnemonic1.addTextChangedListener(JumpTextWatcher(tvMnemonic1, tvMnemonic2))
        tvMnemonic2.addTextChangedListener(JumpTextWatcher(tvMnemonic2, tvMnemonic3))
        tvMnemonic3.addTextChangedListener(JumpTextWatcher(tvMnemonic3, tvMnemonic4))
        tvMnemonic4.addTextChangedListener(JumpTextWatcher(tvMnemonic4, tvMnemonic5))
        tvMnemonic5.addTextChangedListener(JumpTextWatcher(tvMnemonic5, tvMnemonic6))
        tvMnemonic6.addTextChangedListener(JumpTextWatcher(tvMnemonic6, tvMnemonic7))
        tvMnemonic7.addTextChangedListener(JumpTextWatcher(tvMnemonic7, tvMnemonic8))
        tvMnemonic8.addTextChangedListener(JumpTextWatcher(tvMnemonic8, tvMnemonic9))
        tvMnemonic9.addTextChangedListener(JumpTextWatcher(tvMnemonic9, tvMnemonic10))
        tvMnemonic10.addTextChangedListener(JumpTextWatcher(tvMnemonic10, tvMnemonic11))
        tvMnemonic11.addTextChangedListener(JumpTextWatcher(tvMnemonic11, tvMnemonic12))
        tvMnemonic12.addTextChangedListener(JumpTextWatcher(tvMnemonic12, mEdPasswd))
        btnImportMnemonic.setOnClickListener {

            if (tvMnemonic1.text.toString().replace(" ","") .isNotEmpty() &&
                    tvMnemonic2.text.toString() .replace(" ","").isNotEmpty() &&
                    tvMnemonic3.text.toString() .replace(" ","").isNotEmpty() &&
                    tvMnemonic4.text.toString() .replace(" ","").isNotEmpty() &&
                    tvMnemonic5.text.toString() .replace(" ","").isNotEmpty() &&
                    tvMnemonic6.text.toString() .replace(" ","").isNotEmpty() &&
                    tvMnemonic7.text.toString() .replace(" ","").isNotEmpty() &&
                    tvMnemonic8.text.toString() .replace(" ","").isNotEmpty() &&
                    tvMnemonic9.text.toString() .replace(" ","").isNotEmpty() &&
                    tvMnemonic10.text.toString() .replace(" ","").isNotEmpty() &&
                    tvMnemonic11.text.toString() .replace(" ","").isNotEmpty() &&
                    tvMnemonic12.text.toString() .replace(" ","").isNotEmpty()) {
                mc = tvMnemonic1.text.toString() .replace(" ","") + " " +
                        tvMnemonic2.text.toString() .replace(" ","") + " " +
                        tvMnemonic3.text.toString() .replace(" ","") + " " +
                        tvMnemonic4.text.toString() .replace(" ","") + " " +
                        tvMnemonic5.text.toString() .replace(" ","") + " " +
                        tvMnemonic6.text.toString() .replace(" ","") + " " +
                        tvMnemonic7.text.toString() .replace(" ","") + " " +
                        tvMnemonic8.text.toString() .replace(" ","") + " " +
                        tvMnemonic9.text.toString() .replace(" ","") + " " +
                        tvMnemonic10.text.toString() .replace(" ","") + " " +
                        tvMnemonic11.text.toString() .replace(" ","") + " " +
                        tvMnemonic12.text.toString().replace(" ","")
            } else {
                Toast.makeText(mContext, "请检查助记词是否完整", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

//            if (getMemory().isEmpty()) {
//                super.onError("请输入助记词")
//
//                return@setOnClickListener
//            }

            if (getPwd().isEmpty()) {
                super.onError("请输入密码")
                return@setOnClickListener
            }

            if (getRePwd().isEmpty()) {
                super.onError("请确认密码")
                return@setOnClickListener
            }

            if (getPwd() != getRePwd()) {
                super.onError("两次输入的密码不一致")
                return@setOnClickListener
            }

            showLoading()
            getPresenter().importWalleByMnemonic()
//            if (OdinWallet.importMemory(getMemory(), getPwd())) {
//                OcWalletListActivity.startWalletListActivity(mContext)
//            } else {
//                super.onError("助记词或密码错误")
//            }
        }
    }

    private fun getMemory(): String {
//        return mEdMemory.text.toString().trim()
        return mc
    }

    private fun getPwd(): String {
        return mEdPasswd.text.toString().trim()
    }

    private fun getRePwd(): String {
        return mEdRePasswd.text.toString().trim()
    }


    override fun initData() {
    }

    override fun createPresenter(): ImportWalletPresenter? {
        return ImportWalletPresenter()
    }

    override fun createView(): ImportWalletView? {
        return this
    }

    override fun getContent(): String {
        return getMemory()
    }

    override fun getPassword(): String {
        return getPwd()
    }

    private var walletAddress: String = ""
    override fun importSuccess(wallet: ETHWallet) {
        hideLoading()
        OcWalletDetailActivity.startOcWalletDetailActivity(mContext, wallet)
        activity?.finish()

        //导入钱包成功之后 保存address
        saveMoneyAddress(wallet.address)
        saveWalletName(wallet.name)
        walletAddress = wallet.address
        getPresenter().login()
    }

    override fun setWalletAddress(): String {
        return walletAddress
    }

    class JumpTextWatcher(vThis: EditText?, vNext: EditText?) : TextWatcher {
        private var mThisView: EditText? = null
        private var mNextView: EditText? = null
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
        override fun afterTextChanged(s: Editable) {
            val str = s.toString()
            if (str.contains(" ")) {
                mNextView!!.requestFocus()
            }
        }

        init {
            mThisView = vThis
            if (vNext != null) {
                mNextView = vNext
            }
        }
    }

}
